//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverSetupLead.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_destination_msg","_die_msg","_shared_destination","_shared_alive","_destination_code","_die_code","_destination","_evt_idx","_is_arrived"
		,"_distance","_action_id","_instance_id","_dest_center","_dest_size","_dest_angle","_dest_rect","_obj_var"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);

if (!isnull _object) then {
	_action_id = [_action] call FFUNC(actionGetID);
	TRACE_1("Action",_action);
	_parameters = [_action] call FFUNC(actionGetParams);

	if (_object isKindOf "Man") then {
		_destination_msg = [_parameters, "DEST_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_die_msg = [_parameters, "DIE_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_shared_destination = [_parameters, "SHARED_DEST_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_shared_alive = [_parameters, "SHARED_ALIVE_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_destination = [_parameters, "DESTINATION", [],THIS_FILE] call FFUNC(hashGet);
		_destination_code = [_parameters, "DEST_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_die_code = [_parameters, "DIE_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_shared_destination = _shared_destination call FFUNC(getStringVar);
		_shared_alive = _shared_alive call FFUNC(getStringVar);

		if (!isNil "_shared_destination") then {
			if (_shared_destination != "") then {
				[_instance_id,_shared_destination,false] spawn FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_destination,false],THIS_FILE] call FFUNC(callEvent);
			};
		};

		if (!isNil "_shared_alive") then {
			if (_shared_alive != "") then {
				[_instance_id,_shared_alive,(alive _object)] spawn FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_alive,(alive _object)],THIS_FILE] call FFUNC(callEvent);
			};
		};

		_evt_idx = _object addEventHandler ["killed", Format["[_this, %1] call %2;",_action_id,QUOTE(FFUNC(eventObjectiveLeadFailed))]];
		[_object,_action,"LEAD_KILLED_EVENT",_evt_idx] call FFUNC(actionSetLocal);
		doStop _object;
		_object setCaptive true;
		_object disableAI "AUTOTARGET";
		_object disableAI "MOVE";
		_object disableConversation true;
		_object setVariable ["BIS_noCoreConversations", true];
		
		_dest_center = GETVAL(_destination,0,[]);
		_dest_size = GETVAL(_destination,1,10);
		_dest_angle = GETVAL(_destination,2,0);
		_dest_rect = GETVAL(_destination,3,false);
		_distance = 10;
		if (IS_STRING(_dest_center)) then {
			_dest_size = markerSize _dest_center;
			_dest_angle = markerDir _dest_center;
			_dest_rect = (markerShape _dest_center == "RECTANGLE");
			_dest_center = getMarkerPos _dest_center;
		};
		if (IS_ARRAY(_dest_size)) then {
			_distance = (_dest_size select 0) max (_dest_size select 1);
		} else {
			_distance = _dest_size;
		};
		_distance = _distance max 7;
		
		_initial_side = OGETVAR(_object,QGVAR(INITIAL_SIDE),"");
		if (_initial_side == "") then { OSETVARP(_object,QGVAR(INITIAL_SIDE),side _object); };
		_obj_var = format["mission_person_arrived_%1",_action_id];
		OSETVARP(_object,_obj_var,false);
		TRACE_1("Unit Var Set",_obj_var);
		// This will trigger the arrival event.
		[_object,_action_id,_dest_center,_distance] spawn {
			private ["_person","_destination","_distance","_action_id"];
			TRACE_1("Lead Destination Check",_this);
			DEFAULT_PARAM(_person,0,objNull);
			DEFAULT_PARAM(_action_id,1,-1);
			DEFAULT_PARAM(_destination,2,(getPos _person));
			DEFAULT_PARAM(_distance,3,10);
			// We check that the current distance between the person and the destination is small enough, plus we check the person is not in a vehicle. Or the person is dead.
			waituntil {sleep 12.3215; ((_distance >= ([(vehicle _person), _destination] call FFUNC(distTo))) OR (! alive _person)); };
			if (alive _person) then {
				if ((vehicle _person != _person) && ((position(vehicle _person)) select 2) > 30) then {
					[QGVAR(all_message),["STR_TMF_TARGET_LEADTOOHIGH","vec",(vehicle _person),"vehicle"],THIS_FILE] call FFUNC(callEvent);
					waituntil {sleep 12.3215; (vehicle _person == _person) || ((position(vehicle _person)) select 2) < 30 || (!alive _person)};
				};
				if (alive _person) then {
					[[_person],_action_id] call FFUNC(eventObjectiveLeadSuccess);
				} else {
					[_person] joinSilent grpNull;
				};
			};
		};
	} else {
		ERROR_1("Object not a man", typeOf _object);
	};
} else {
	ERROR("Object not set");
};
TRACE("End");
TMFPROFILERSTOP;